<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="TypeScript," />










<meta name="description" content="TypeScript编译打包处理1.TypeScript介绍TypeScript 是 JavaScript 的一个超集，支持 ECMAScript 6 标准（ES6 教程） TypeScript 由微软开发的自由和开源的编程语言 TypeScript 设计目标是开发大型应用，它可以编译成纯 JavaScript，编译出来的 JavaScript 可以运行在任何浏览器上 从名字上来说，TypeScr">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript编译打包处理">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/05/21/TypeScript%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="TypeScript编译打包处理1.TypeScript介绍TypeScript 是 JavaScript 的一个超集，支持 ECMAScript 6 标准（ES6 教程） TypeScript 由微软开发的自由和开源的编程语言 TypeScript 设计目标是开发大型应用，它可以编译成纯 JavaScript，编译出来的 JavaScript 可以运行在任何浏览器上 从名字上来说，TypeScr">
<meta property="og:locale">
<meta property="article:published_time" content="2022-05-21T14:14:41.000Z">
<meta property="article:modified_time" content="2022-05-22T12:45:31.711Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="TypeScript">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/05/21/TypeScript编译打包处理/"/>





  <title>TypeScript编译打包处理 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/05/21/TypeScript%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">TypeScript编译打包处理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-05-21T22:14:41+08:00">
                2022-05-21
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/05/21/TypeScript%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/05/21/TypeScript编译打包处理/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="TypeScript编译打包处理"><a href="#TypeScript编译打包处理" class="headerlink" title="TypeScript编译打包处理"></a>TypeScript编译打包处理</h1><h2 id="1-TypeScript介绍"><a href="#1-TypeScript介绍" class="headerlink" title="1.TypeScript介绍"></a>1.TypeScript介绍</h2><p>TypeScript 是 JavaScript 的一个超集，支持 ECMAScript 6 标准（<a target="_blank" rel="noopener" href="https://www.runoob.com/w3cnote/es6-tutorial.html">ES6 教程</a>）</p>
<p>TypeScript 由微软开发的自由和开源的编程语言</p>
<p>TypeScript 设计目标是开发大型应用，它可以编译成纯 JavaScript，编译出来的 JavaScript 可以运行在任何浏览器上</p>
<p>从名字上来说，TypeScript最直观的感受就是多了类型，变量类型。</p>
<p>我们都知道，JavaScript是没有变量类型的，但是TS有，而且TS还有很多增强的功能，这对于大型项目的构建和维护，是非常重要的，而且Vue3全面支持TS，因此学习TS是必须的，加油加油。</p>
<h2 id="2-安装TypeScript"><a href="#2-安装TypeScript" class="headerlink" title="2.安装TypeScript"></a>2.安装TypeScript</h2><ul>
<li>命令行安装TypeScript</li>
</ul>
<p><code>npm install typescript -g</code></p>
<ul>
<li>首先编译ts代码成js</li>
</ul>
<span id="more"></span>

<p><code>tsc index.ts</code></p>
<ul>
<li>使用node执行js代码</li>
</ul>
<p><code>node index.js或者nodemon index.js</code></p>
<h2 id="3-安装相关的插件"><a href="#3-安装相关的插件" class="headerlink" title="3.安装相关的插件"></a>3.安装相关的插件</h2><p><strong>ts-node</strong> 插件的作用，直接编译ts代码成js代码并且执行js代码</p>
<p><code>npm install ts-node -g</code></p>
<p><strong>types/node</strong> 的作用解决编译错误，由于使用ts-node进行ts的编译运行时遇到的错误</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Cannot find name <span class="string">&#x27;console&#x27;</span>. <span class="keyword">Do</span> you need to change your target library? <span class="keyword">Try</span> changing the <span class="string">&#x27;lib&#x27;</span> compiler option to include</span><br></pre></td></tr></table></figure>

<p>安装这个库就可以正常进行编译运行了</p>
<p><code>npm install -g  @types/node</code></p>
<p><strong>知识点：</strong>（来自知乎Yiming Li的解释）</p>
<p>链接如下：</p>
<p><a target="_blank" rel="noopener" href="https://www.zhihu.com/question/470016632">https://www.zhihu.com/question/470016632</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console 不属于 EcmaScript 标准。DOM 里面的 console 是浏览器环境下的，属于浏览器BOM API，Node 里面的 console 是 Node.js 里面的，由nodejs自己定义的API，两者虽然有同样的功能，但是并不是同一个东西，例如你可以参考：如果从 Node.js 编译到浏览器环境， Webpack 本来是需要对 console 进行 polyfill 的，但是恰好有一样的功能，所以不需要。你可以把 console 想成 fs 就是一样的道理了，你不装 @types/node 根本无法使用 fs 的类型提示。 因此总结，主要原因 TypeScript 自带的类型只有 DOM 环境里面的那些类型（在 lib.dom.d.ts 里面），Node.js 的类型是不自带并且通过 @types/node 这个库来维护的。</span><br></pre></td></tr></table></figure>

<h2 id="4-配置打包文件（tsc自带工具和webpack）"><a href="#4-配置打包文件（tsc自带工具和webpack）" class="headerlink" title="4.配置打包文件（tsc自带工具和webpack）"></a>4.配置打包文件（tsc自带工具和webpack）</h2><ul>
<li>为什么要配置？</li>
</ul>
<p>因为我们每次更改项目里面很多文件的时候，我们希望它能够自动把编译好的文件输出到某一指定的目录下</p>
<ul>
<li>怎么配置？</li>
</ul>
<p>首先我们使用命令</p>
<p><strong>tsc -init</strong> 初始化一个tsconfig.json配置文件</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;include&quot;</span>: [<span class="string">&quot;./src/**/*&quot;</span>],</span><br><span class="line">  <span class="attr">&quot;compilerOptions&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;target&quot;</span>: <span class="string">&quot;es2016&quot;</span>,                                  <span class="comment">/* Set the JavaScript language version for emitted </span></span><br><span class="line"><span class="comment">    &quot;module&quot;: &quot;commonjs&quot;,                                /* Specify what module code is generated. */</span>      </span><br><span class="line">    <span class="attr">&quot;outDir&quot;</span>: <span class="string">&quot;dist&quot;</span>,</span><br><span class="line"><span class="comment">//    &quot;outFile&quot;: &quot;./dist/app.js&quot;,</span></span><br><span class="line">    <span class="attr">&quot;strict&quot;</span>: <span class="literal">true</span>,                                      <span class="comment">/* Enable all strict type-checking options. */</span></span><br><span class="line">    <span class="attr">&quot;skipLibCheck&quot;</span>: <span class="literal">true</span>                                 <span class="comment">/* Skip type checking all .d.ts files. */</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>可以在这里指定打包的目标目录以及文件名</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/TypeScript/" rel="tag"># TypeScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/05/02/nginx%E9%85%8D%E7%BD%AEssl%E4%B9%8B%E8%B8%A9%E5%9D%91%E7%B3%BB%E5%88%97/" rel="next" title="nginx配置ssl之踩坑系列">
                <i class="fa fa-chevron-left"></i> nginx配置ssl之踩坑系列
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/06/01/JS%E5%B7%A5%E5%85%B7%E7%B1%BB/" rel="prev" title="JS工具类">
                JS工具类 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#TypeScript%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86"><span class="nav-number">1.</span> <span class="nav-text">TypeScript编译打包处理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-TypeScript%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.</span> <span class="nav-text">1.TypeScript介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%89%E8%A3%85TypeScript"><span class="nav-number">1.2.</span> <span class="nav-text">2.安装TypeScript</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AE%89%E8%A3%85%E7%9B%B8%E5%85%B3%E7%9A%84%E6%8F%92%E4%BB%B6"><span class="nav-number">1.3.</span> <span class="nav-text">3.安装相关的插件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E9%85%8D%E7%BD%AE%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6%EF%BC%88tsc%E8%87%AA%E5%B8%A6%E5%B7%A5%E5%85%B7%E5%92%8Cwebpack%EF%BC%89"><span class="nav-number">1.4.</span> <span class="nav-text">4.配置打包文件（tsc自带工具和webpack）</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/05/21/TypeScript%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E5%A4%84%E7%90%86/';
          this.page.identifier = '2022/05/21/TypeScript编译打包处理/';
          this.page.title = 'TypeScript编译打包处理';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
